//
//  EditPersonControl.m
//  FreeTime
//
//  Created by 王江华 on 16/8/26.
//  Copyright © 2016年 SRDZ.com. All rights reserved.

#import "EditPersonControl.h"
#import "Reachability.h"
#import "Header.h"
#import "AccountControl.h"
#import "editInfoCell.h"
#import "editInterCell.h"
#import "BindAliPayControl.h"
@interface EditPersonControl (){

 NSInteger _checkIndex;     //选择照片

}
@property (nonatomic,strong) UIImageView                    *picFi,*picSe,*picTh,*picFo,*picFive,*picSix;
@property (nonatomic,strong) UIImage                        *img1,*img2,*img3,*img4,*img5,*img6;

@property(nonatomic,strong)UIView *nameView;

@property(nonatomic,strong)UIImageView  *sexImg;

@property(nonatomic,strong)UILabel *nameLabel,*metalLabel;

@property(nonatomic,strong)UIImageView *jiantouImg;

@property(nonatomic,strong)NSMutableArray      *headAlbum;

@property (nonatomic,assign) BOOL            haveRemoveImage;

@property(nonatomic,strong)NSArray  *userArr;

@property(nonatomic,strong)NSArray *UserArray;
@end

@implementation EditPersonControl

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
     self.tableView.tableHeaderView=[self setupHeader];
     self.navigationItem.leftBarButtonItem=[UIBarButtonItem  setupBarButtonItemWithText:@"保存" Font:14 Target:self action:@selector(saveAction)];
     self.navigationItem.rightBarButtonItem=[UIBarButtonItem setupBarButtonItemWithText:@"取消" Font:14 Target:self action:@selector(cancelAciton)];
    
     [self dataInit];
    
}
-(void)saveAction{

    [self.navigationController popViewControllerAnimated:YES];

}
-(void)cancelAciton{

    BindAliPayControl *ali=[[BindAliPayControl alloc]init];
    
    [self.navigationController pushViewController:ali animated:YES];
}
-(void)dataInit{

    
    self.userArr=[NSMutableArray array];
    
    self.userArr=@[@[@"线上技能"],@[@"线下技能"],@[@"行业",@"公司",@"地区",@"经常出没",@"个性签名"],@[@"我的爱好",@"喜欢的应用",@"我喜欢的运动",@"我喜欢的音乐",@"我喜欢的书"],@[@"微信"]];
    
    self.UserArray=[NSMutableArray array];
    
    self.UserArray=@[@"线上技能",@"线下技能",@"我的信息",@"我的兴趣",@"我的社交账号"];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.userArr.count;
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    NSArray *Nar=self.userArr[section];
    
    return  Nar.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    NSArray *arr= self.userArr[indexPath.section];
    NSString *str=arr[indexPath.row];
    if ([str  isEqualToString:@"行业"]){
        
        editInfoCell *infoCell=[editInfoCell cellWithTableView:tableView];
        
        infoCell.propertyLabel.text=@"行业";
    
        return infoCell;
    }
  else  if ([str  isEqualToString:@"公司"]) {
        
        editInfoCell *infoCell=[editInfoCell cellWithTableView:tableView];
      
        infoCell.propertyLabel.text=@"公司";
        
        return infoCell;
    }
 else   if ([str  isEqualToString:@"地区"]) {
        
        editInfoCell *infoCell=[editInfoCell cellWithTableView:tableView];
     
        infoCell.propertyLabel.text=@"地区";
        
        return infoCell;
    }
 else   if ([str  isEqualToString:@"经常出没"]) {
        
        editInfoCell *infoCell=[editInfoCell cellWithTableView:tableView];
     
        infoCell.propertyLabel.text=@"经常出没";
        
        return infoCell;
}
 else   if ([str  isEqualToString:@"个性签名"]) {
        
        editInfoCell *infoCell=[editInfoCell cellWithTableView:tableView];
     
        infoCell.propertyLabel.text=@"个性签名";
        
        return infoCell;
     
    }
 else   if ([str  isEqualToString:@"微信"]) {
     
     
     editInfoCell *infoCell=[editInfoCell cellWithTableView:tableView];
     
     infoCell.propertyLabel.text=@"微信";
     
     return infoCell;
     
 }
 else if ([str isEqualToString:@"我的爱好"]){
 
     editInterCell *infoCell=[editInterCell cellWithTableView:tableView];
     
     infoCell.propertyLabel.text=@"我的爱好";
     
     infoCell.imageImg.image=[UIImage imageNamed:@"aihao"];
     
     return infoCell;
     
 }
 else if ([str isEqualToString:@"喜欢的应用"]){
     
     editInterCell *infoCell=[editInterCell cellWithTableView:tableView];
     
     infoCell.propertyLabel.text=@"喜欢的应用";
     
     infoCell.imageImg.image=[UIImage imageNamed:@"yingyong"];
     
     return infoCell;
 }
    
 else if ([str isEqualToString:@"我喜欢的运动"]){
 
     editInterCell *infoCell=[editInterCell cellWithTableView:tableView];
     
     infoCell.propertyLabel.text=@"我喜欢的运动";
     
     infoCell.imageImg.image=[UIImage imageNamed:@"yundong"];
     
     return infoCell;
 
 }
 else if ([str isEqualToString:@"我喜欢的音乐"]){
     editInterCell *infoCell=[editInterCell cellWithTableView:tableView];
     
     infoCell.propertyLabel.text=@"我喜欢的音乐";
     
     infoCell.imageImg.image=[UIImage imageNamed:@"yinyue"];
     
     return infoCell;
 
 }
 else if ([str isEqualToString:@"我喜欢的书"]){
 
     editInterCell *infoCell=[editInterCell cellWithTableView:tableView];
     
     infoCell.propertyLabel.text=@"我喜欢的书";
     
     infoCell.imageImg.image=[UIImage imageNamed:@"shu"];
     
     return infoCell;
     
 }
 else{
     
    editInfoCell *infoCell=[editInfoCell cellWithTableView:tableView];

    infoCell.propertyLabel.text=@"行业";
    
    return infoCell;
     
}
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 45;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 5;
}
/*-(NSString*)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    if (section==0) {
        
        return @"线上技能";
    }
    else if(section==1){
        
        return @"线下技能";
    }
    else if(section==2){
        
        return @"我的信息";
    }
    else if(section==3){
        
        return @"我的标签";
    }
    else {
        
        return @"我的社交账号";
    }
}*/
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView * bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 30)];
    UILabel * onlineLbl = [ReduceCode creatLabelWithFrame:CGRectMake(10, 10, 120, 15) title:self.UserArray[section] titleColor:[ImageUtil colorWithRgb:0x4c4c4c] font:14 textAlignment:0];
    [bgView addSubview:onlineLbl];
    [ToolUtil createLine:CGRectGetMaxY(onlineLbl.frame)+2 superView:bgView lift:10 width:60 hight:1 color:[ImageUtil colorWithRgb:0xb3b3b3]];
    return bgView;
}
-(UIView *)setupHeader{

    
    /** 定义照片尺寸 */
    CGFloat ImgSizeSma = (WindowWidth-20)/3;
    CGFloat ImgSizeBig = (WindowWidth-15) - ImgSizeSma;
    
    UIView *headerView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, WindowWidth+67)];
    headerView.backgroundColor=[UIColor clearColor];
    if (!_picFi) {
        _picFi=[[UIImageView alloc]initWithFrame:CGRectMake(5, 5, ImgSizeBig, ImgSizeBig)];
        _picFi.tag = 1;
        _picFi.image=[UIImage imageNamed:@"tianjiaupian"];
        _picFi.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]
                                     initWithTarget:self
                                     action:@selector(CheckImgFi)];
        [_picFi addGestureRecognizer:tap];
        [headerView addSubview:_picFi];
    }
    if (!_picSe) {
        _picSe=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_picFi.frame)+5, 5, ImgSizeSma, ImgSizeSma)];
        _picSe.image=[UIImage imageNamed:@"tianjiaupian"];
        _picSe.tag = 2;
        _picSe.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap1=[[UITapGestureRecognizer alloc]
                                      initWithTarget:self
                                      action:@selector(CheckImgSe)];
        [_picSe addGestureRecognizer:tap1];
        [headerView addSubview:_picSe];
    }
    if (!_picTh) {
        _picTh=[[UIImageView alloc]initWithFrame:CGRectMake(_picSe.frame.origin.x, CGRectGetMaxY(_picSe.frame)+5, ImgSizeSma, ImgSizeSma)];
        _picTh.tag = 3;
        _picTh.image=[UIImage imageNamed:@"tianjiaupian"];
        
        _picTh.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap2=[[UITapGestureRecognizer alloc]
                                      initWithTarget:self
                                      action:@selector(CheckImgTh)];
        [_picTh addGestureRecognizer:tap2];
        [headerView addSubview:_picTh];
    }
    if (!_picFo) {
        _picFo=[[UIImageView alloc]initWithFrame:CGRectMake(_picSe.frame.origin.x, CGRectGetMaxY(_picTh.frame)+5, ImgSizeSma, ImgSizeSma)];
        _picFo.tag = 4;
        _picFo.image=[UIImage imageNamed:@"tianjiaupian"];
        _picFo.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap3=[[UITapGestureRecognizer alloc]
                                      initWithTarget:self
                                      action:@selector(CheckImgFo)];
        [_picFo addGestureRecognizer:tap3];
        [headerView addSubview:_picFo];
    }
    if (!_picSix) {
        _picSix=[[UIImageView alloc]initWithFrame:CGRectMake(5, CGRectGetMaxY(_picFi.frame)+5, ImgSizeSma, ImgSizeSma)];
        _picSix.tag = 6;
        _picSix.image=[UIImage imageNamed:@"tianjiaupian"];
        _picSix.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap5=[[UITapGestureRecognizer alloc]
                                      initWithTarget:self
                                      action:@selector(CheckImgSix)];
        [_picSix addGestureRecognizer:tap5];
        [headerView addSubview:_picSix];
    }
    if (!_picFive) {
        _picFive=[[UIImageView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(_picSix.frame)+5, CGRectGetMaxY(_picFi.frame)+5, ImgSizeSma, ImgSizeSma)];
        _picFive.tag = 5;
        _picFive.image=[UIImage imageNamed:@"tianjiaupian"];
        _picFive.userInteractionEnabled=YES;
        UITapGestureRecognizer *tap4=[[UITapGestureRecognizer alloc]
                                      initWithTarget:self
                                      action:@selector(CheckImgFive)];
        [_picFive addGestureRecognizer:tap4];
        [headerView addSubview:_picFive];
    }
    if (!_nameView) {
        
        _nameView=[[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(_picFive.frame)+5, WindowWidth, 65)];
        
        _nameView.userInteractionEnabled=YES;
        
        UITapGestureRecognizer *nameTap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(editNameAction)];
        
        
        [_nameView addGestureRecognizer:nameTap];
        
        _sexImg=[[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 15, 15)];
        
        _sexImg.contentMode=UIViewContentModeScaleAspectFill;
        
        
        _sexImg.image=[UIImage imageNamed:@"nvsheng"];
    
        _nameLabel=[[UILabel alloc]initWithFrame:CGRectMake(40, 15, 150, 15)];
        
        _nameLabel.font=[UIFont systemFontOfSize:14];
        
        _nameLabel.textColor=[UIColor redColor];
        
        _nameLabel.text=@"我是一只鱼       21";
        
        _metalLabel=[[UILabel alloc]initWithFrame:CGRectMake(10, CGRectGetMaxY(_nameLabel.frame)+4, 200, 15)];
        _metalLabel.font=[UIFont systemFontOfSize:9];
        
        _metalLabel.text=@"点击编辑账户信息：姓名,年龄";
        
        _metalLabel.textColor=[ImageUtil colorWithRgb:0xcccccc];
        
        _jiantouImg=[[UIImageView alloc]initWithFrame:CGRectMake(WindowWidth-40, 20, 20, 20)];
        
        _jiantouImg.image=[UIImage imageNamed:@"youjiantoutou"];
        
        [headerView addSubview:_nameView];
        [_nameView addSubview:_sexImg];
        [_nameView addSubview:_nameLabel];
        [_nameView addSubview:_metalLabel];
        [_nameView addSubview:_jiantouImg];
    }
    Reachability *reachable = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    if ([reachable currentReachabilityStatus] == NotReachable){
        
        UIImage *image1 = [self getImageFromDocuments:HEADIMG1];
        UIImage *image2 = [self getImageFromDocuments:HEADIMG2];
        UIImage *image3 = [self getImageFromDocuments:HEADIMG3];
        UIImage *image4 = [self getImageFromDocuments:HEADIMG4];
        UIImage *image5 = [self getImageFromDocuments:HEADIMG5];
        UIImage *image6 = [self getImageFromDocuments:HEADIMG6];
        if (image1) {
            _picFi.image = image1;
        }
        if (image2) {
            _picSe.image = image2;
            UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(deleteImage:)];
            [_picSe addGestureRecognizer:longGes];
        }
        if (image3) {
            _picTh.image = image3;
            UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(deleteImage:)];
            [_picTh addGestureRecognizer:longGes];
        }
        if (image4) {
            _picFo.image = image4;
            UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(deleteImage:)];
            [_picFo addGestureRecognizer:longGes];
        }
        if (image5) {
            _picFive.image = image5;
            UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(deleteImage:)];
            [_picFive addGestureRecognizer:longGes];
        }
        if (image6) {
            _picSix.image = image6;
            UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(deleteImage:)];
            [_picSix addGestureRecognizer:longGes];
        }
    }else{
        for (int i=0; i<self.headAlbum.count; i++) {
            UIImageView *imageView = (UIImageView *)[headerView viewWithTag:(i+1)];
            NSString *str = self.headAlbum[i];
            if ([str isEqualToString:@"00"]) {
                imageView.image = [UIImage imageNamed:@"releaseback"];
            }else{
                [imageView sd_setImageWithURL:[NSURL URLWithString:str]];
               
                if (i >= 0) {
                    UILongPressGestureRecognizer *longGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(deleteImage:)];
                    [imageView addGestureRecognizer:longGes];
                }
            }
        }
    }
    return headerView;

}
-(void)editNameAction{

        AccountControl *AccountVc=[[AccountControl  alloc]init];
    
       [self.navigationController pushViewController:AccountVc animated:YES];
    
    
    
    
    
}
-(void)deleteImage:(UILongPressGestureRecognizer *)longGes{

    
    
    
    
}
-(void)CheckImgFi
{
    _checkIndex = 0;
    [self AtapGestures];
}

-(void)CheckImgSe
{
    _checkIndex = 1;
    [self AtapGestures];
}

-(void)CheckImgTh
{
    _checkIndex = 2;
    [self AtapGestures];
}

-(void)CheckImgFo
{
    _checkIndex = 3;
    [self AtapGestures];
}

-(void)CheckImgFive
{
    _checkIndex = 4;
    [self AtapGestures];
}

-(void)CheckImgSix
{
    _checkIndex = 5;
    [self AtapGestures];
}
-(void)AtapGestures{


    
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    //获取沙盒路径
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    [imageData writeToFile:fullPath atomically:NO];
}
-(UIImage *)getImageFromDocuments:(NSString*)imageName
{
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    NSData *data = [NSData dataWithContentsOfFile:fullPath];
    return [UIImage imageWithData:data];
}

- (void)removeImageWithName:(NSString *)imageName
{
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    NSError *error = nil;
    if([[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error]) {
        //DLog(@"文件移除成功");
    } else {
       // DLog(@"error=%@", error);
    }
}
-(NSString *)getStringUrl:(NSMutableArray *)array
{
    if (array.count == 0) {
        return @"";
    }else{
        NSMutableArray *SignArr = array;
        NSString *SignSTR = [[NSString alloc]init];
        NSString *SignStr0 = SignArr[0];
        for (int i=1; i < SignArr.count; i++) {
            NSString *SignStr = [NSString stringWithFormat:@"%@%@",@",",SignArr[i]];
            SignSTR = [SignSTR stringByAppendingString:SignStr];
        }
        SignSTR = [SignStr0 stringByAppendingString:SignSTR];
        return SignSTR;
    }
}
@end
